home *** CD-ROM | disk | FTP | other *** search
/ APC & TCP 4 / APC & TCP 4.iso / games / publicdomain / a / attacks / sources / thinker.mod < prev    next >
Text File  |  1994-05-14  |  39KB  |  972 lines

  1. IMPLEMENTATION MODULE thinker;
  2.  
  3. (*$R-V-*)        (* Range checking OFF, overflow checking OFF *)
  4.  
  5. (*   This module handles all the routines necessary for the computer to   *)
  6. (* to play.  It also has some routines that are convenient for checking   *)
  7. (* to see if the human player screwed up or not.                          *)
  8.  
  9. FROM attacksgraphics
  10.   IMPORT   ChangePointer, DrawSquare, mywindowptr;
  11. FROM header
  12.   IMPORT   state, movetype, boardrange, boardtype, playertype, squaretype,
  13.            allmovestype, difficulty, pointercode, gameover, currentpointer,
  14.            backedup, maxmovetypemoves;
  15. FROM mdgenerallib
  16.   IMPORT   RealRandom, MyPause;
  17. FROM Intuition
  18.   IMPORT   IDCMPFlags, IntuiMessagePtr, MENUNUM, ITEMNUM;
  19. FROM Ports
  20.   IMPORT   GetMsg, ReplyMsg;
  21. FROM RandomNumbers
  22.   IMPORT   Random;
  23. FROM TermInOut
  24.   IMPORT   WriteString, WriteLn, WriteCard, WriteInt;
  25.  
  26.  
  27. CONST
  28.   winnumber  =  100;            (* This is number returned for a WIN.  *)
  29.   losenumber = -100;            (* This one's for a LOSE.              *)
  30.   ranpick = 0.1;                (* This tells how smart it is for diff *)
  31.                                 (*  level 2.                           *)
  32.   maxmoves = 400;               (* The maximun number of moves genera- *)
  33.                                 (*  ted per level.                     *)
  34.   numwhich = 5;                 (* Tells how many guesses the computer *)
  35.                                 (*  gets when doing difficulty 2.      *)
  36.  
  37. VAR
  38.   interrupt : BOOLEAN;          (* When this is TRUE, then the compu-  *)
  39.                                 (*  ter's move is to be aborted.       *)
  40.   imessageptr : IntuiMessagePtr;   (* Points to an intuimessage     *)
  41.  
  42. (************************************************************************)
  43. PROCEDURE OtherPlayer (player : playertype) : playertype;
  44.  
  45. (*   Simply returns the other player     *)
  46. BEGIN
  47.   IF player = red THEN
  48.      RETURN blue;
  49.      END;
  50.   RETURN red;
  51. END OtherPlayer;
  52.  
  53. (************************************************************************)
  54. PROCEDURE LegalMove (move : movetype) : BOOLEAN;
  55.  
  56. (*   This simply checks to see if the given move is a valid move, con- *)
  57. (* sidering the current state of the game.  It is NOT assumed that     *)
  58. (* the initial component of the move is valid.  It does NOT assume     *)
  59. (* both of the move components are valid locations.  But it does as-   *)
  60. (* sume that the board has been properly initialized so that the outer *)
  61. (* edges are blocks.  It operates using the global variable, state.    *)
  62.  
  63. VAR
  64.   legal : BOOLEAN;
  65.   checkx, checky : boardrange;     (* used to see if moves are in range *)
  66.  
  67. BEGIN
  68.   legal := FALSE;
  69.   IF (state.board[move.fromX, move.fromY] = state.turn) AND
  70.      (state.board[move.toX,move.toY] = empty) THEN
  71.      IF move.toX > move.fromX THEN
  72.         checkx := move.toX - move.fromX;
  73.         ELSE checkx := move.fromX - move.toX;
  74.         END;
  75.      IF move.toY > move.fromY THEN
  76.         checky := move.toY - move.fromY;
  77.         ELSE checky := move.fromY - move.toY;
  78.         END;
  79.      IF (checkx < 3) AND (checky < 3) THEN
  80.         legal := TRUE;
  81.         END;
  82.      END;
  83.  
  84.   RETURN legal;
  85. END LegalMove;
  86.  
  87.  
  88. (**************************************************************************)
  89. PROCEDURE FindAllMoves (VAR board : boardtype; player : playertype;
  90.                         VAR moves : allmovestype);
  91.  
  92. (*   This procedure finds all the possible moves for player in the posi-  *)
  93. (* tion of the given board.  The resultant moves are stored in the vari-  *)
  94. (* able, moves.  If there are no moves possible, then moves.nummoves = 0. *)
  95. (*                                                                        *)
  96. (*   INPUT                                                                *)
  97. (*            board                Of boardtype.  It describes the state  *)
  98. (*                                 of the board in question.              *)
  99. (*                                                                        *)
  100. (*            player               This tells which side (player) this    *)
  101. (*                                 procedure checks the moves for.        *)
  102. (*                                                                        *)
  103. (*   OUTPUT                                                               *)
  104. (*            moves                The data structure that holds all the  *)
  105. (*                                 moves.                                 *)
  106. VAR
  107.   i, j : boardrange;
  108.  
  109. BEGIN
  110.   moves.nummoves := 0;
  111.   FOR i := 1 TO 7 DO   FOR j := 1 TO 7 DO
  112.      IF board[i,j] = player THEN
  113.         IF (moves.nummoves < maxmoves) AND (board[i-2,j-2] = empty) THEN
  114.            IF moves.nummoves = maxmovetypemoves THEN
  115.               RETURN;
  116.               END;
  117.            INC(moves.nummoves);
  118.            moves.moves[moves.nummoves].fromX := i;
  119.            moves.moves[moves.nummoves].fromY := j;
  120.            moves.moves[moves.nummoves].toX := i - 2;
  121.            moves.moves[moves.nummoves].toY := j - 2;
  122.            END;
  123.         IF (moves.nummoves < maxmoves) AND (board[i-2,j-1] = empty) THEN
  124.            IF moves.nummoves = maxmovetypemoves THEN
  125.               RETURN;
  126.               END;
  127.            INC(moves.nummoves);
  128.            moves.moves[moves.nummoves].fromX := i;
  129.            moves.moves[moves.nummoves].fromY := j;
  130.            moves.moves[moves.nummoves].toX := i - 2;
  131.            moves.moves[moves.nummoves].toY := j - 1;
  132.            END;
  133.         IF (moves.nummoves < maxmoves) AND (board[i-2,j] = empty) THEN
  134.            IF moves.nummoves = maxmovetypemoves THEN
  135.               RETURN;
  136.               END;
  137.            INC(moves.nummoves);
  138.            moves.moves[moves.nummoves].fromX := i;
  139.            moves.moves[moves.nummoves].fromY := j;
  140.            moves.moves[moves.nummoves].toX := i - 2;
  141.            moves.moves[moves.nummoves].toY := j;
  142.            END;
  143.         IF (moves.nummoves < maxmoves) AND (board[i-2,j+1] = empty) THEN
  144.            IF moves.nummoves = maxmovetypemoves THEN
  145.               RETURN;
  146.               END;
  147.            INC(moves.nummoves);
  148.            moves.moves[moves.nummoves].fromX := i;
  149.            moves.moves[moves.nummoves].fromY := j;
  150.            moves.moves[moves.nummoves].toX := i - 2;
  151.            moves.moves[moves.nummoves].toY := j + 1;
  152.            END;
  153.         IF (moves.nummoves < maxmoves) AND (board[i-2,j+2] = empty) THEN
  154.            IF moves.nummoves = maxmovetypemoves THEN
  155.               RETURN;
  156.               END;
  157.            INC(moves.nummoves);
  158.            moves.moves[moves.nummoves].fromX := i;
  159.            moves.moves[moves.nummoves].fromY := j;
  160.            moves.moves[moves.nummoves].toX := i - 2;
  161.            moves.moves[moves.nummoves].toY := j + 2;
  162.            END;
  163.         IF (moves.nummoves < maxmoves) AND (board[i-1,j-2] = empty) THEN
  164.            IF moves.nummoves = maxmovetypemoves THEN
  165.               RETURN;
  166.               END;
  167.            INC(moves.nummoves);
  168.            moves.moves[moves.nummoves].fromX := i;
  169.            moves.moves[moves.nummoves].fromY := j;
  170.            moves.moves[moves.nummoves].toX := i - 1;
  171.            moves.moves[moves.nummoves].toY := j - 2;
  172.            END;
  173.         IF (moves.nummoves < maxmoves) AND (board[i-1,j-1] = empty) THEN
  174.            IF moves.nummoves = maxmovetypemoves THEN
  175.               RETURN;
  176.               END;
  177.            INC(moves.nummoves);
  178.            moves.moves[moves.nummoves].fromX := i;
  179.            moves.moves[moves.nummoves].fromY := j;
  180.            moves.moves[moves.nummoves].toX := i - 1;
  181.            moves.moves[moves.nummoves].toY := j - 1;
  182.            END;
  183.         IF (moves.nummoves < maxmoves) AND (board[i-1,j] = empty) THEN
  184.            IF moves.nummoves = maxmovetypemoves THEN
  185.               RETURN;
  186.               END;
  187.            INC(moves.nummoves);
  188.            moves.moves[moves.nummoves].fromX := i;
  189.            moves.moves[moves.nummoves].fromY := j;
  190.            moves.moves[moves.nummoves].toX := i - 1;
  191.            moves.moves[moves.nummoves].toY := j;
  192.            END;
  193.         IF (moves.nummoves < maxmoves) AND (board[i-1,j+1] = empty) THEN
  194.            IF moves.nummoves = maxmovetypemoves THEN
  195.               RETURN;
  196.               END;
  197.            INC(moves.nummoves);
  198.            moves.moves[moves.nummoves].fromX := i;
  199.